package bsc.war.handler;

import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.Pointer;
import bsc.ejb.entity.Project;
import bsc.ejb.entity.ScoreCard;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author castrosalazar
 */
public class MainLoadData extends AbstractController {

    public MainLoadData() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            List<ScoreCard> scoreCards = delegate.findScoreCard();

            //List<DimensionalCut> dimensionalCuts = ScoreCardUtils.getDimensionalCuts(request, delegate);
            List<Pointer> pointers = new ArrayList<Pointer>();
            for (ScoreCard scoreCard : scoreCards) {

                if (scoreCard instanceof Pointer && !(scoreCard instanceof DimensionalCut)) {
                    pointers.add((Pointer) scoreCard);
                }
            }

            List<Project> projects = new ArrayList<Project>();

            for (ScoreCard scoreCard : scoreCards) {
                if (scoreCard instanceof Project) {
                    projects.add((Project) scoreCard);
                }
            }

            /*
            for (ScoreCard scoreCard : scoreCards) {
            if(scoreCard instanceof Pointer){
            pointers.add((Pointer)scoreCard);
            }else if(scoreCard instanceof Project){
            projects.add((Project)scoreCard);
            }
            }
             */

       
            request.setAttribute("pointers", pointers);
            request.setAttribute("projects", projects);

            if (request.getParameter("data") != null && !request.getParameter("data").isEmpty()) {
                request.setAttribute("action", request.getParameter("data"));
            } else {
                request.setAttribute("action", "");
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Caught exception", e);
        }
        return new ModelAndView("mainLoadData");
    }
}
